class ProfilesController < ApplicationController
  before_action :set_profile, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!
  respond_to :html, :json

  def index
    @profiles = Profile.where(:user_id => current_user.id)
    respond_with(@profiles)
  end

  def show
    session[:current_profile] = @profile.id
    @asset_acc = @profile.accounts.find_by_name('Asset')
    @credit_acc = @profile.accounts.find_by_name('Credit')
    respond_with(@profile)
  end

  def new
    @profile = Profile.new
  end

  def edit
  end

  def create
    @profile = Profile.new(profile_params)
    @profile.user = current_user
    flash[:notice] = 'Profile was successfully created.' if @profile.save

    @account = Account.new
    @account.name = @profile.name
    @account.profile = @profile
    @account.name = 'Asset'
    @account.save
        
    @account = Account.new
    @account.name = @profile.name
    @account.profile = @profile
    @account.name = 'Credit'
    @account.save
    respond_with(@profile)
  end

  def update
    flash[:notice] = 'Profile was successfully updated.' if @profile.update(profile_params)
    respond_with(@profile)
  end

  def destroy
    flash[:notice] = 'Profile was successfully destroyed.' if @profile.destroy
    respond_with(@profile)
  end

  def report

  end


  private
    def set_profile
      @profile = Profile.find(params[:id])
    end

    def profile_params
      params[:profile].permit(:name, :gender, :born, :weight, :height, :height_feet, :height_inches)
    end
end
